home *** CD-ROM | disk | FTP | other *** search
- /*
- File: UseRsrcM.cpp
-
- Contains: Functions for using resources from the resource fork of a library
-
- Owned by: Jens Alfke
-
- Copyright: © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
-
- */
-
-
- #ifndef _USERSRCM_
- #include "UseRsrcM.h"
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h"
- #endif
-
- #ifndef _ODMEMORY_
- #include <ODMemory.h>
- #endif
-
- #ifndef _PASCLSTR_
- #include <PasclStr.h>
- #endif
-
- #ifndef __FRAGRSRC__
- #include <FragRsrc.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
-
- static CFragResFileRef gFragRef = kODNULL; // Reference to resource map
- static short gRefNum = -1; // RefNum of resource file
- static ODSLong gNesting = 0; // Nesting level (>0 means in use)
-
-
- OSErr
- InitLibraryResources( CFragInitBlockPtr init )
- {
- return InitCFragResources(init,&gFragRef);
- }
-
-
- void
- CloseLibraryResources( )
- {
- if( gNesting>0 )
- WARN("Resource file still in use while being closed!");
- OSErr err= TermCFragResources(gFragRef);
- if( err )
- WARN("TermCFragResources returned %hd",err);
-
- gFragRef = kODNULL;
- gRefNum = -1;
- gNesting = 0;
- }
-
-
- ODSLong BeginUsingLibraryResources( )
- {
- short ref = CurResFile();
- if( gNesting > 0 )
- UseResFile(gRefNum);
- else {
- THROW_IF_ERROR( BeginCFragResources(gFragRef) );
- gRefNum = CurResFile();
- }
- gNesting++;
- return ref;
- }
-
-
- void EndUsingLibraryResources( ODSLong ref )
- {
- if( gNesting > 0 )
- if( --gNesting == 0 )
- THROW_IF_ERROR( EndCFragResources(gFragRef) );
- UseResFile(ref);
- }
-
-
- CUsingLibraryResources::~CUsingLibraryResources( )
- {
- ODSLong ref = fRefNum;
- if( ref != -1 ) { // Make sure it's done only once
- fRefNum = -1;
- EndUsingLibraryResources(ref);
- }
- }
-
-
- // RESOURCE UTILITIES:
-
-
- static void*
- BasicReadResource( ResType type, short id, ConstStr255Param name, ODBoolean asHandle )
- {
- Handle rsrc;
- void *result = kODNULL; ODVolatile(result);
-
- ODSLong savedRef = BeginUsingLibraryResources();
-
- SetResLoad(false);
- if( name )
- rsrc = Get1NamedResource(type,name);
- else
- rsrc = Get1Resource(type,id);
- SetResLoad(true);
- if( rsrc==kODNULL ) {
- OSErr err = ResError();
- EndUsingLibraryResources(savedRef);
- THROW( err ?err :resNotFound );
- }
-
- TRY{
- ODSize size = (*rsrc) ?GetHandleSize(rsrc) :GetResourceSizeOnDisk(rsrc);
- void *dst;
- if( asHandle ) {
- result = (void*) ODNewHandle(size);
- dst = ODLockHandle((ODHandle)result);
- } else
- result = dst = ODNewPtr(size);
-
- if( *rsrc == kODNULL ) {
- // Resource is not in memory, use partial-resource call:
- ReadPartialResource( rsrc, 0, dst, size );
- THROW_IF_ERROR( ResError() );
- ReleaseResource(rsrc);
- } else {
- // Resource is already in memory; just copy it:
- ODBlockMove(*rsrc,dst,size);
- }
-
- if( asHandle )
- ODUnlockHandle((ODHandle)result);
-
- }CATCH_ALL{
- if( *rsrc == kODNULL )
- ReleaseResource(rsrc);
- EndUsingLibraryResources(savedRef);
- if( result )
- if( asHandle )
- ODDisposeHandle((ODHandle)result);
- else
- ODDisposePtr(result);
- RERAISE;
- }ENDTRY
-
- EndUsingLibraryResources(savedRef);
- return result;
- }
-
-
- ODHandle
- ODReadResource( ResType type, short id )
- {
- return (ODHandle) BasicReadResource(type,id,kODNULL,kODTrue);
- }
-
- ODHandle
- ODReadNamedResource( ResType type, ConstStr255Param name )
- {
- if( name==kODNULL )
- THROW(kODErrIllegalNullInput);
- return (ODHandle) BasicReadResource(type,0,name,kODTrue);
- }
-
- ODPtr
- ODReadResourceToPtr( ResType type, short id )
- {
- return BasicReadResource(type,id,kODNULL,kODFalse);
- }
-
- ODPtr
- ODReadNamedResourceToPtr( ResType type, ConstStr255Param name )
- {
- if( name==kODNULL )
- THROW(kODErrIllegalNullInput);
- return BasicReadResource(type,0,name,kODFalse);
- }
-
-
- void
- ODGetString( Str255 str, short id )
- {
- str[0] = 0;
- ODSLong savedRef = BeginUsingLibraryResources();
- Handle h = Get1Resource('STR ',id);
- OSErr result = ResError();
- if( h ) {
- CopyPascalString(str,(StringPtr)*h);
- ReleaseResource(h);
- }
- EndUsingLibraryResources(savedRef);
- if( !h ) {
- THROW(result);
- THROW(resNotFound);
- }
- }
-
-
- void
- ODGetIndString( Str255 str, short id, short index )
- {
- str[0] = 0;
- ODSLong savedRef = BeginUsingLibraryResources();
- Handle h = Get1Resource('STR#',id);
- OSErr result = ResError();
- if( h ) {
- #if ODDebug
- if( index<1 || index>**(short**)h ) {
- WARN("ODGetIndString(%hd,%hd): invalid index",id,index);
- result= kODErrValueOutOfRange;
- } else
- #endif
- GetIndString(str,id,index);
- ReleaseResource(h);
- }
- EndUsingLibraryResources(savedRef);
- if( result ) {
- THROW(result);
- THROW(resNotFound);
- }
- }
-